package com.um.jdy.tcp.rpc.impl;


import com.alibaba.fastjson.JSON;
import com.um.jdy.rpc.entity.xh.EBWirelessDeviceParams;
import com.um.jdy.tcp.downlink.*;
import com.um.jdy.rpc.api.eb.RpcEBXhTCPDeviceService;
import com.um.jdy.rpc.entity.EBDeviceInfo;
import com.um.jdy.rpc.entity.EBDeviceParams;
import com.um.jdy.rpc.entity.xh.EBXhDeviceParams;
import com.um.jdy.tcp.cmd.CommandSender;
import com.um.jdy.tcp.properties.HshNettyProperties;
import com.um.jdy.tcp.utils.SeqUtil;
import com.um.springcloud.dubbo.cluster.DeviceKey;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 邓南辉
 * 日期 2023/5/22
 * 版权所有 广州优匠科技有限公司
 */
@Slf4j
@Service
@DubboService
public class RpcXhEBDeviceServiceImpl implements RpcEBXhTCPDeviceService {

    //@Autowired
    //protected BicycleIotService bicycleIotService;

    @Autowired
    CommandSender commandSender;

    @Autowired
    HshNettyProperties hshNettyProperties;


    /**
     * 请求启动充电
     * @param seq 序列号
     * @param imei IMEI码
     * @param port 端口号
     * @param duration 充电时长(秒)
     */
    @Override
    public String reqStartCharge(String seq, DeviceKey imei, Integer port, Integer duration,Integer overPower,Integer payAmount){

        Cmd03StartChargeReq req = new Cmd03StartChargeReq();
        req.setSeq(SeqUtil.getSeqNo());
        req.setDevice(imei.getKey());
        req.setPort(port);
        req.setPower(overPower);
        req.setTime(duration);

        try {
            commandSender.sendCmd(imei.getKey(),req);
        }catch (Exception ex){
            log.error("回复失败-{}:{}","Cmd03StartChargeReq",req);

            return "发送失败："+ JSON.toJSONString(req);
        }

        return JSON.toJSONString(req);
    }

    /**
     * 请求更新充电时间
     *
     * @param seq        序列号
     * @param imei IMEI码
     * @param port       端口号
     * @param duration   充电时长(秒)
     */
    @Override
    public String reqUpdateCharge(String seq, DeviceKey imei, Integer port, Integer duration,Integer overPower) {
        Cmd03StartChargeReq req = new Cmd03StartChargeReq();
        req.setSeq(SeqUtil.getSeqNo());
        req.setDevice(imei.getKey());
        req.setPort(port);
        req.setPower(overPower);
        req.setTime(duration);

        try {
            commandSender.sendCmd(imei.getKey(),req);
        }catch (Exception ex){
            log.error("回复失败-{}:{}","Cmd03StartChargeReq",req);

            return "发送失败："+JSON.toJSONString(req);
        }

        return JSON.toJSONString(req);
    }

    /**
     * 请求停止充电
     * @param seq 序列号
     * @param imei IMEI码
     * @param port 端口号
     */
    @Override
    public String reqStopCharge(String seq,DeviceKey imei,Integer port){
        Cmd04StopChargeReq req = new Cmd04StopChargeReq();
        req.setSeq(SeqUtil.getSeqNo());
        req.setDevice(imei.getKey());
        req.setPort(port);

        try {
            commandSender.sendCmd(imei.getKey(),req);
        }catch (Exception ex){
            log.error("回复失败-{}:{}","Cmd04StopChargeReq",req);
            return "发送失败："+JSON.toJSONString(req);
        }

        return JSON.toJSONString(req);
    }


    /**
     * 请求查询端口状态
     * @param seq 序列号
     * @param imei IMEI码
     */
    @Override
    public String reqQueryPorts(String seq,DeviceKey imei){
        Cmd05QuerySocketReq req = new Cmd05QuerySocketReq();
        req.setSeq(SeqUtil.getSeqNo());
        req.setDevice(imei.getKey());

        try {
            commandSender.sendCmd(imei.getKey(),req);
        }catch (Exception ex){
            log.error("回复失败-{}:{}","Cmd05QuerySocketReq",req);
            return "发送失败："+JSON.toJSONString(req);
        }

        return JSON.toJSONString(req);
    }


    /**
     * 请求下发语音播放
     * @param seq 序列号
     * @param imei IMEI码
     * @param voice 播放文字
     */
    @Override
    public String reqPlayTTS(String seq,DeviceKey imei,String voice){
        Cmd09TTSReq req = new Cmd09TTSReq();
        req.setSeq(SeqUtil.getSeqNo());
        req.setDevice(imei.getKey());
        req.setVoice(voice);

        try {
            commandSender.sendCmd(imei.getKey(),req);
        }catch (Exception ex){
            log.error("回复失败-{}:{}","Cmd09TTSReq",req);
            return "发送失败："+JSON.toJSONString(req);
        }

        return JSON.toJSONString(req);
    }


    /**
     * 请求下发设备重启
     * @param seq 序列号
     * @param imei IMEI码
     */
    @Override
    public String reqRestart(String seq,DeviceKey imei){
        Cmd10RestartReq req = new Cmd10RestartReq();
        req.setSeq(SeqUtil.getSeqNo());
        req.setDevice(imei.getKey());

        try {
            commandSender.sendCmd(imei.getKey(),req);
        }catch (Exception ex){
            log.error("回复失败-{}:{}","Cmd10RestartReq",req);
            return "发送失败："+JSON.toJSONString(req);
        }

        return JSON.toJSONString(req);
    }


    /**
     * 请求下发参数设置
     * @param seq 序列号
     * @param imei IMEI码
     * @param params 设备参数
     */
    @Override
    public String reqSetParams(String seq,DeviceKey imei,EBDeviceParams params){
        if(params instanceof EBXhDeviceParams) {
            EBXhDeviceParams xhParams = (EBXhDeviceParams)params;

            Cmd11ParamsReq req = new Cmd11ParamsReq();
            req.setSeq(SeqUtil.getSeqNo());
            req.setDevice(imei.getKey());
            req.setMoney(xhParams.getMoney());
            req.setOffTime1(xhParams.getOffTime1());
            req.setOffTime2(xhParams.getOffTime2());
            req.setPower(xhParams.getPower());
            req.setTemp(xhParams.getTemp());
            try {
                commandSender.sendCmd(imei.getKey(), req);
            } catch (Exception ex) {
                log.error("回复失败-{}:{}", "Cmd11ParamsReq", req);
                return "发送失败：" + JSON.toJSONString(req);
            }
            return JSON.toJSONString(req);
        }else if(params instanceof EBWirelessDeviceParams){
            EBWirelessDeviceParams xhParams = (EBWirelessDeviceParams)params;

            Cmd11WirelessParamsReq req = new Cmd11WirelessParamsReq();
            req.setSeq(SeqUtil.getSeqNo());
            req.setDevice(imei.getKey());
            req.setMaxVoltage(xhParams.getMaxVoltage());
            req.setMaxCurrent(xhParams.getMaxCurrent());
            req.setMinCurrent(xhParams.getMinCurrent());
            req.setTemp(xhParams.getTemp());
            req.setValue1(xhParams.getValue1());
            req.setValue2(xhParams.getValue2());
            try {
                commandSender.sendCmd(imei.getKey(), req);
            } catch (Exception ex) {
                log.error("回复失败-{}:{}", "Cmd11WirelessParamsReq", req);
                return "发送失败：" + JSON.toJSONString(req);
            }
            return JSON.toJSONString(req);
        }

        return "";
    }



    /**
     * 回复设备登录
     * @param seq 序列号
     * @param imei IMEI码
     * @param info 设备信息对象
     */
    @Override
    public String respDeviceLogin(String seq, DeviceKey imei, EBDeviceInfo info){
        Cmd00LoginResp resp = new Cmd00LoginResp();
        resp.setSeq(seq);
        resp.setDevice(imei.getKey());
        resp.setServerType("TCP");
        resp.setAddress(hshNettyProperties.getAddress());
        resp.setPort(hshNettyProperties.getPort()+"");
        resp.setHeart(info.getHeart());
        resp.setMqttUser("");
        resp.setMqttPass("");
        resp.setRevise(0);
        try {
            log.info("回复登录：{}-{}",imei,resp);
            commandSender.sendCmd(imei.getKey(),resp);
        }catch (Exception ex){
            log.error("回复失败-{}:{}","Cmd00LoginResp",JSON.toJSONString(resp));
            return "发送失败："+JSON.toJSONString(resp);
        }

        return JSON.toJSONString(resp);
    }


    /**
     * 回复设备心跳
     * @param seq 序列号
     * @param imei IMEI码
     */
    @Override
    public String respDeviceHeart(String seq,DeviceKey imei){
        Cmd01HeartResp resp = new Cmd01HeartResp();
        resp.setSeq(seq);
        resp.setDevice(imei.getKey());
        try {
            log.info("回复心跳：{}-{}",imei,JSON.toJSONString(resp));
            commandSender.sendCmd(imei.getKey(),resp);
        }catch (Exception ex){
            log.error("回复失败-{}:{}","Cmd01HeartResp",resp);
            return "发送失败："+JSON.toJSONString(resp);
        }

        return JSON.toJSONString(resp);
    }


    /**
     * 回复设备端口上报
     * @param seq 序列号
     * @param imei IMEI码
     */
    @Override
    public String respPortsNotify(String seq,DeviceKey imei){
        Cmd02SocketNofityResp resp = new Cmd02SocketNofityResp();
        resp.setSeq(seq);
        resp.setDevice(imei.getKey());
        try {
            commandSender.sendCmd(imei.getKey(),resp);
        }catch (Exception ex){
            log.error("回复失败-{}:{}","Cmd02SocketNofityResp",resp);
            return "发送失败："+JSON.toJSONString(resp);
        }

        return JSON.toJSONString(resp);
    }

    /**
     * 回复充电数据
     *
     * @param seq        序列号
     * @param imei       IMEI码
     */
    @Override
    public String respChargingData(String seq, DeviceKey imei) {
        return "";
    }


    /**
     * 回复刷卡充电
     * @param seq 序列号
     * @param imei IMEI码
     * @param port 端口
     * @param duration 充电时长（秒）
     * @param power 过载功率
     */
    @Override
    public String respSlotCard(String seq,DeviceKey imei,Integer port,Integer duration,Integer power){
        Cmd06SlotCardResp resp = new Cmd06SlotCardResp();
        resp.setSeq(seq);
        resp.setDevice(imei.getKey());
        resp.setPort(port);
        resp.setTime(duration);
        resp.setPower(power);
        try {
            commandSender.sendCmd(imei.getKey(),resp);
        }catch (Exception ex){
            log.error("回复失败-{}:{}","Cmd06SlotCardResp",resp);
            return "发送失败："+JSON.toJSONString(resp);
        }

        return JSON.toJSONString(resp);
    }


    /**
     * 回复投币充电
     * @param seq 序列号
     * @param imei IMEI码
     */
    @Override
    public String respInsertCoin(String seq,DeviceKey imei){
        Cmd07InsertCoinResp resp = new Cmd07InsertCoinResp();
        resp.setSeq(seq);
        resp.setDevice(imei.getKey());
        try {
            commandSender.sendCmd(imei.getKey(),resp);
        }catch (Exception ex){
            log.error("回复失败-{}:{}","Cmd07InsertCoinResp",resp);
            return "发送失败："+JSON.toJSONString(resp);
        }

        return JSON.toJSONString(resp);
    }

    /**
     * 发送JSON参数
     *
     * @param seq        序列号
     * @param imei       IMEI码
     * @param jsonParams     设备参数
     */
    @Override
    public String reqSendCommand(String seq, DeviceKey imei, String jsonParams) {
        DownlinkCommand cmd = null;
        try {
            cmd = DownlinkCommandFactory.createCommand(jsonParams);
            log.info("发送JSON参数-{}:{}","reqSendCommand",cmd);
            commandSender.sendCmd(imei.getKey(),cmd);
        }catch (Exception ex){
            log.error("发送JSON参数-{}:{}","reqSendCommand",cmd);
            return "发送JSON参数："+JSON.toJSONString(cmd);
        }
        return "";
    }
}
